package com.wd.health.view.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wd.health.R;
import com.wd.health.model.adapter.CjbzAdapter01;
import com.wd.health.model.adapter.CjbzAdapter02;
import com.wd.health.model.bean.CjbzBean02;
import com.wd.health.model.bean.KslbBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.view.activity.home.CjbzxqActivity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CjbzFragment extends Fragment {

    private RecyclerView rv01;
    private RecyclerView rv02;
    private int mid = 7;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cjbz, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rv01 = view.findViewById(R.id.rv_cjbz01);
        rv02 = view.findViewById(R.id.rv_cjbz02);
        rv01.setLayoutManager(new LinearLayoutManager(getContext()));
        rv02.setLayoutManager(new GridLayoutManager(getContext(),2));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initCjbz();
    }

    private void initCjbz2() {
        RetrofitManager.getInstance().getCjbz02Data(mid, new Observer<CjbzBean02>() {

            private List<CjbzBean02.ResultBean> result;

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CjbzBean02 cjbzBean02) {
                result = cjbzBean02.getResult();
                CjbzAdapter02 cjbzAdapter02 = new CjbzAdapter02(result, getActivity());
                rv02.setAdapter(cjbzAdapter02);
                cjbzAdapter02.notifyDataSetChanged();
                cjbzAdapter02.setGetListener(new CjbzAdapter02.GetListener() {
                    @Override
                    public void onClick(String name, int id) {
                        Intent intent = new Intent(getActivity(), CjbzxqActivity.class);
                        intent.putExtra("id",id);
                        intent.putExtra("name",name);
                        startActivity(intent);
                    }

                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initCjbz() {
        RetrofitManager.getInstance().getKslbData(new Observer<KslbBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(KslbBean kslbBean) {
                List<KslbBean.ResultBean> list = kslbBean.getResult();
                final CjbzAdapter01 cjbzAdapter01 = new CjbzAdapter01(list, getContext());
                rv01.setAdapter(cjbzAdapter01);
                initCjbz2();
                cjbzAdapter01.setGetListener(new CjbzAdapter01.GetListener() {
                    @Override
                    public void onClick(int position, int id) {
                        cjbzAdapter01.setmPosition(position);
                        mid = id;
                        initCjbz2();
                        cjbzAdapter01.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }
}